package cn.joyfulmedia.common.util;

import cn.joyfulmedia.common.dto.APIException;
import cn.joyfulmedia.common.dto.ResultDO;
import cn.joyfulmedia.common.result.CommonResultCodeEnum;
import cn.joyfulmedia.common.result.ModuleResultCode;
import cn.joyfulmedia.common.result.ResultCodeInterface;

/**
 * Created by loumingshan on 2016/11/9.
 */
public class ResultUtils {
    /**
     * 只检查返回是否成功还是失败,resultCode代表调用哪个服务的错误
     *
     * @param resultDO
     */
    public static void checkResultWithoutModle(ResultDO resultDO, ResultCodeInterface resultCode) {
        if (resultDO == null) {
            throw new APIException(resultCode);
        }
        if (!resultDO.isSuccess()) {

            throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
        }

    }

    /**
     * 只检查返回是否成功还是失败,resultCode代表调用哪个服务的错误
     *
     * @param resultDO
     */
    public static void checkResultWithoutModle(ResultDO resultDO, ModuleResultCode moduleResultCode) {
        if (resultDO == null) {
            int resultCode = moduleResultCode.getCode() + CommonResultCodeEnum.RESULT_IS_NULL_ERROR.getResultCode();
            throw new APIException(resultCode, CommonResultCodeEnum.RESULT_IS_NULL_ERROR.getMessage());
        }
        if (!resultDO.isSuccess()) {

            throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
        }

    }

    /**
     * 接口使用检查返回是否成功还是失败,resultCode代表调用哪个服务的错误,msg接口错误信息统一封装
     *
     * @param resultDO
     * @param msg
     */
    public static void checkResultWithoutModleWithMsg(ResultDO resultDO, ResultCodeInterface resultCode, String msg) {
        if (resultDO == null) {
            throw new APIException(resultCode.getResultCode(), msg);
        }
        if (!resultDO.isSuccess()) {

            throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
        }

    }

    /**
     * 使用检查返回是否成功还是失败,resultCode代表调用哪个服务的错误,并且检查result model是否为空
     *
     * @param resultDO
     * @param resultCode
     */
    public static void checkResultWithModle(ResultDO resultDO, ResultCodeInterface resultCode) {
        if (resultDO == null) {
            throw new APIException(resultCode);
        }
        if (!resultDO.isSuccess()) {

            throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
        }
        if (resultDO.getModule() == null) {
            throw new APIException(resultCode);

        }
    }

    /**
     * 使用检查返回是否成功还是失败,resultCode代表调用哪个服务的错误,并且检查result model是否为空
     *
     * @param resultDO
     * @param moduleResultCode
     */
    public static void checkResultWithModle(ResultDO resultDO, ModuleResultCode moduleResultCode) {
        if (resultDO == null) {
            int resultCode = moduleResultCode.getCode() + CommonResultCodeEnum.RESULT_IS_NULL_ERROR.getResultCode();
            throw new APIException(resultCode, CommonResultCodeEnum.RESULT_IS_NULL_ERROR.getMessage());
        }
        if (!resultDO.isSuccess()) {

            throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
        }
        if (resultDO.getModule() == null) {
            int resultCode = moduleResultCode.getCode() + CommonResultCodeEnum.RESULT_DATA_IS_NULL_ERROR.getResultCode();
            throw new APIException(resultCode, CommonResultCodeEnum.RESULT_DATA_IS_NULL_ERROR.getMessage());
        }
    }

    /**
     * 使用检查返回是否成功还是失败,resultCode代表调用哪个服务的错误,并且检查result model是否为空,msg接口错误信息统一封装
     *
     * @param resultDO
     * @param resultCode
     * @param msg
     */
    public static void checkResultWithModleWithMsg(ResultDO resultDO, ResultCodeInterface resultCode, String msg) {
        if (resultDO == null) {
            throw new APIException(resultCode.getResultCode(), msg);
        }
        if (!resultDO.isSuccess()) {

            throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
        }
        if (resultDO.getModule() == null) {
            throw new APIException(resultCode.getResultCode(), msg);

        }
    }
}
